package com.monitor.service.impl;

import com.monitor.dao.AdminDao;
import com.monitor.po.Administrator;
import com.monitor.service.AdminService;
import com.monitor.util.CommenUtil;
import com.monitor.util.Constants;
import com.monitor.util.EncoderUtil;

/**
 * @author zzy
 * 
 */
public class AdminServiceImpl implements AdminService {
	private AdminDao adminDao;

	/**
	 * 添加管理员
	 */
	@Override
	public boolean addAdministator(Administrator administrator) throws Exception {
		// 判断管理员是否已经存在
		Administrator admin = adminDao.getAdministratorByName(administrator.getUsername());
		if (admin == null) {
			String password = administrator.getPassword();
			// MD5加密密码
			password = EncoderUtil.encode(password, Constants.ENCODE_MD5);
			administrator.setPassword(password);
			adminDao.addAdministrator(administrator);
			return true;
		}
		return false;
	}

	/**
	 * 管理员登陆
	 */
	@Override
	public Administrator login(String username, String password) throws Exception {
		if (!CommenUtil.isEmpty(username) && !CommenUtil.isEmpty(password)) {
			Administrator admin = adminDao.getAdministratorByName(username);
			if (admin != null && admin.getPassword().equals(EncoderUtil.encode(password, Constants.ENCODE_MD5))) {
				return admin;
			}
		}
		return null;
	}

	/**
	 * 修改密码
	 */
	@Override
	public void changePassword(Administrator administrator) throws Exception {
		adminDao.updateAdministrator(administrator);
	}

	/**
	 * @param adminDao
	 *            the adminDao to set
	 */
	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

}
